package tingshu.servlet.album;

import tingshu.repository.AlbumRepo;
import tingshu.util.Log;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

/**
 * @author houdaiqin
 * @date 2022/07/18 15:01
 **/
@WebServlet("/studio/album/create.do")
public class CreateDoServlet extends HttpServlet {
    private final AlbumRepo albumRepo=new AlbumRepo();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String title = req.getParameter("title");
        String cover = req.getParameter("cover");
        if (title==null||title.equals("")){
            throw new ServletException("专辑名称为空");
        }
        if (cover==null||cover.equals("")){
            throw new ServletException("专辑封面为空");
        }
        int state=1;

        UserVO currentUser=null;
        HttpSession session=req.getSession(false);
        if (session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");
        }
        if (currentUser==null){
            Log.println("用户未登录，请登录后使用");
            return;
        }

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");

        albumRepo.insertInToAlbum(currentUser.uid,title,cover,state);

        PrintWriter writer=resp.getWriter();
        //writer.println("创建专辑成功");
        resp.sendRedirect("/studio/album/list.html");
    }
}